package com.sunyard.manage.initRunner;

import com.sunyard.constant.ErrMessageConst;
import com.sunyard.dal.mapper.PubConfigMapper;
import com.sunyard.manage.service.OrganizationService;
import com.sunyard.manage.util.xmlUti.XmlParseUtil;
import com.sunyard.redisUtil.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

//import com.sunyard.utils.esb.HandleREquest;

@Component
@Slf4j
public class LoadRunner implements CommandLineRunner {

    @Value("${spring.datasource.dynamic.datasource.master.url}")
    private String datasourceUrl;

    @Value("${spring.datasource.dynamic.datasource.master.username}")
    private String datasourceUserName;

    @Value("${spring.cloud.nacos.discovery.server-addr}")
    private String nacosHost;

    @Value("${spring.profiles.active}")
    private String profilesActive;

    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private OrganizationService organizationService;

    @Value("${spring.cloud.client.ip-address}")
    private String ipAddress;

    @Value("${server.port}")
    private Integer serverPort;

//    @Value("${jsrcu.bank.organization}")
//    private String organization;

    @Autowired
    private PubConfigMapper pubConfigMapper;

    @Override
    public void run(String... args) throws Exception {

        log.info("====服务启动args:" + Arrays.toString(args));
        log.info("====启动配置:" + profilesActive);
        log.info("====数据库URL:" + datasourceUrl);
        log.info("====数据库USER:" + datasourceUserName);
        log.info("====nacos地址:" + nacosHost);
        log.info("生成的ipAddress:{}", ipAddress);
        log.info("生成的serverPort:{}", serverPort);
        this.init();
//        HandleREquest.init();
    }

    @Async
    public void init() throws Exception {
        //获取机器码 初始化serviceSnUtils
        this.initSn();
        //加载xml文件
//        log.info("加载xml");
//        XmlParseUtil.loadXmlList("/xml/*/");
//        log.info("加载完毕");

        List<String> listInitOrgIds = new ArrayList<>();
//        listInitOrgIds.add(organization);
//        listInitOrgIds.add("32009990");
        for (String orgId : listInitOrgIds) {
            String strkey = "orgid:" + orgId;
            List lists = redisUtils.get(strkey, List.class);
            if (lists == null) {
                organizationService.getOrgIds(orgId);
            }

            String strkey2 = "orgtreeid:" + orgId;
            List lists2 = redisUtils.get(strkey2, List.class);
            if (lists2 == null) {
                organizationService.obtainOrgTree(orgId);
            }


        }

        String strkey4 = "businessScope:businessScope";
        List lists4 = redisUtils.get(strkey4, List.class);
//        if(lists4 == null){
//            generalInfoService.getBusinessScope();
//        }
    }

    @Async
    public void initSn() {
        try {
//            String machineIpPort = ipAddress + ":" + serverPort;
//            String machineId = pubConfigMapper.selectMachineSn(machineIpPort);
//            if(StringUtils.isBlank(machineId)){
//                pubConfigMapper.insertMachineSn(machineIpPort);
//            }
//            machineId = pubConfigMapper.selectMachineSn(machineIpPort);
//            ServiceSnUtils.setInstance(machineId);
//            log.info("初始化serviceSnUtils成功");
        } catch (Exception e) {
            log.error(ErrMessageConst.SYSTEM_ERROR, e);
            log.error("初始化serviceSn失败");
            //退出程序
            throw e;
        }
    }

}

